/**   
 * @Title: Diagram.java 
 * @Package org.playgod1984.du.ddesign.model 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author playgod playgod1984@163.com  
 * @date 2014年10月27日 下午3:23:13 
 * @version V1.0   
 */
package org.playgod1984.du.ddesign.model;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.playgod1984.du.base.util.DuDesignUtil;

import com.jfinal.log.Log4jLogger;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

/** 
 * @ClassName: Diagram 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author playgod playgod1984@163.com 
 * @date 2014年10月27日 下午3:23:13 
 *  
 */
@SuppressWarnings("all")
public class Diagram extends Model<Diagram> {
	private static final Logger log = Log4jLogger.getLogger(Diagram.class);
	public static final Diagram dao = new Diagram();
	/**
	 * @Title: findByName
	 * @author:playgod playgod1984@163.com 
	 * @Description: 获取同名图表 
	 * @param @param diagramname
	 * @param @return    设定文件 
	 * @return Diagram    返回类型 
	 * @throws
	 */
	public Diagram findByName(Map params){		
		String sql="select diagramid from "
				+ "	dud_design_diagram where diagramname='"+params.get("diagramname")+"'"
						+ " AND projectid='"+ params.get("projectid") +"'";
		return findFirst(sql);
	}
	/**
	 * @Title: findByNameWithDiffCode
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return Diagram    返回类型 
	 * @throws
	 */
	public Diagram findByNameWithDiffCode(Map params){		
		String sql="select diagramid from "
				+ "	dud_design_diagram where diagramname='"+params.get("diagramname")+"'"
						+ " AND projectid !='"+ params.get("projectid") +"'"
						+ " AND diagramid !='"+ params.get("diagramid") +"'";
		return findFirst(sql);
	}
	/**
	 * @Title: save
	 * @author:playgod playgod1984@163.com 
	 * @Description: 保存
	 * @param @param params
	 * @param @return    设定文件 
	 * @return boolean    返回类型 
	 * @throws
	 */
	public boolean save(Map params){
		Record r = new Record();
		r.set("diagramname", params.get("diagramname"));
		r.set("projectid", params.get("projectid"));
		r.set("remark", params.get("remark"));
		r.set("updator", params.get("currentUser"));
		r.set("updatetime",new Date());
		try{
			if("A".equals((String)params.get("screen"))){//ADD
				r.set("diagramid", DuDesignUtil.getUUID32());
				r.set("creator", params.get("currentUser"));
				r.set("createtime",new Date());
				Db.save("dud_design_diagram", r);
			}else{
				r.set("diagramid", params.get("diagramid"));
				Db.update("dud_design_diagram", "diagramid", r);
			}
			return true;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return false;
		}
	}
	/**
	 * @Title: deleteRows
	 * @author:playgod playgod1984@163.com 
	 * @Description: TODO(这里用一句话描述这个方法的作用) 
	 * @param @param params
	 * @param @return    设定文件 
	 * @return int    返回类型 
	 * @throws
	 */
	public int deleteRows(Map params){
		int results =0;
		try{
			String[] ids = StringUtils.split((String)params.get("keys"),",");
			for(int i=0;i<ids.length;i++){
				if(dao.deleteById(ids[i])){
					results++;
				}
			}
			return results;
		}catch(Exception ex){
			log.error(ex.getMessage());
			return -1;
		}
	}
	
}
